package Invalid;

import java.awt.*;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;
import java.security.*;

public class LoreServer2 {
	private final int PORT = 2222;
	private ServerSocket serverSocket = null;
	
	
	public LoreServer2()
	{
		listenTo( PORT );
		runServer();

	}
	
	private void listenTo( int port )
	{
		// Listen to a certain port
		try 
		{
            this.serverSocket = new ServerSocket(port);
        } 
		catch (IOException e) 
		{
        	System.err.println("Could not listen on port: " + port );
            System.exit(1);
        }
		System.out.println("Successfully listen on port: " + port );
	}
	
	private void runServer()  {
		System.out.println("Server runs successfully!");
		 while(true){
			    ClientWorker w;
			    try{
			    	//server.accept returns a client connection
			    	Socket socket1 = serverSocket.accept();
			    	w = new ClientWorker(socket1 , null );
					Thread t = new Thread(w);
				    t.start();
			     
			    } catch (IOException e) {
			      System.out.println("Accept failed: 2222");
			      System.exit(-1);
			    }
		 }
	}
	
	public static void main (String[] args) throws Exception 
	{
		LoreServer2 newServer = new LoreServer2();
    }
	
}
